const util = require("/util/util");
const requsts = require("/util/requst");
const debounce = require("/util/debounce");
Page({
  data: {
    name:"",
    tell:"",
    addres:"",
    checked:0,
    clear:true,
    userId:"",
    shippingaddress:"",  //收货地址
    detailedaddress:"",   //详细地址
    orderId:"",
    cardnum:"",
    zhifbtell:"",
    issub:true,
  },
  onItemInput(e){
    this.setData({
      [e.target.dataset.field]: e.detail.value,
    });
  },
  onClear(e){
       this.setData({
      [e.target.dataset.field]: '',
    });
  },
  //地图
  onExtraTap(){
    var that = this
    my.chooseLocation({
         success:(res)=>{
          console.log(JSON.stringify(res))
          //my.alert({content:JSON.stringify(res)})
          //my.alert({content:JSON.stringify(res.provinceName+res.cityName+res.adName)})
          if(res.provinceName==undefined&&res.cityName==undefined&&res.adName==undefined){
            that.setData({
              detailedaddress:res.address
            })
          }else{
            that.setData({
              shippingaddress:(res.provinceName+res.cityName+res.adName),
              detailedaddress:res.address
            })
          }
          
        },
        fail:(error)=>{
          //my.alert({content: '调用失败：'+JSON.stringify(error), });
        },
    });
  },
  onBlur(e){
      this.setData({
        detailedaddress:e.detail.value
      })
      if(!this.data.detailedaddress){
       util.toastfn("请填写详细地址")
       return
     }
  }, 
  submit(){
     if(!this.data.name){
       util.toastfn("请填写姓名")
       return
     }
     if(!this.data.cardnum){
       util.toastfn("请填写身份证号")
       return
     }
     if(!this.data.zhifbtell){
       util.toastfn("请填写用户支付宝绑定手机号")
       return
     }
     if(this.data.zhifbtell.length<11){
       util.toastfn("请输入正确支付宝绑定手机号")
       return
     }
     if(!util.isPositiveInteger(this.data.zhifbtell)){
       util.toastfn("请输入正确支付宝绑定手机号")
       return
     }
     if(!this.data.tell){
       util.toastfn("请输入手机号码")
       return
     }
     if(this.data.tell.length<11){
       util.toastfn("请输入正确手机号码")
       return
     }
     if(!util.isPositiveInteger(this.data.tell)){
       util.toastfn("请输入正确手机号码")
       return
     }
     if(!this.data.shippingaddress){
       util.toastfn("请填写收货地址")
       return
     }
     if(!this.data.detailedaddress){
       util.toastfn("请填写详细地址")
       return
     }
    //  if(!this.data.issub){
    //     return
    // }
  
    var param={
      userId:this.data.userId,
      name:this.data.name,
      shippingPhone:this.data.tell,
      shippingAddress:this.data.shippingaddress,
      detailedAddress:this.data.detailedaddress,
      idCard:this.data.cardnum,
      phone:this.data.zhifbtell,
      orderId:this.data.orderId,
    }
    var that=this
    my.request({
      url: requsts.url+"/user/supplementUserInfo",
      method: 'POST',
      headers: {'content-type': 'application/x-www-form-urlencoded'},
      data:param,
      dataType: 'json',
      success: function(res) {
        console.log(res)
        if(res.data.code==0){
          my.navigateTo({
            url:'/page/common/sureorder/sureorder?orderId='+that.data.orderId,
          })
					
        }else{
          util.toastfn(res.data.msg)
        }
      },
      fail: function(res) {
          util.toastfn(res.errorMessage)
      },
    });
  },
  switch2Change(e){
    if(e.detail.value){
        this.setData({
       checked:1
     })
    }else{
      this.setData({
       checked:0
     })
    }
     console.log(this.data.checked)
  },
  getUserDefaultAddress(){
    var that=this
    var param={
      userId:that.data.userId
    }
      my.request({
      url: requsts.url+"/user/getUserDefaultAddress",
      method: 'POST',
      headers: {'content-type': 'application/x-www-form-urlencoded'},
      data:param,
      dataType: 'json',
      success: function(res) {
        console.log(res)
        if(res.data.code==0){
          if(res.data.data!=undefined){
              that.setData({
              tell:res.data.data.shippingPhone,
              shippingaddress:res.data.data.shippingAddress,
              detailedaddress:res.data.data.detailedAddress,
            })
          }
            
        }else{
          util.toastfn(res.data.msg)
        }
      },
      fail: function(res) {
          util.toastfn(res.errorMessage)
      },
    });
  },
  onLoad(e){
    this.setData({
      orderId:e.orderId
    })
    var that=this
    my.getStorage({
      key: 'userId',
      success: function(res) {
        that.setData({
          userId:res.data.userId,
        })
        that.getUserDefaultAddress()
      },
      fail: function(res){
        util.toastfn(res.errorMessage)
      }
    });
  },
});